package com.example.com.order.service.impl;

import com.example.com.order.entity.Order;
import com.example.com.order.fegin.StockFeignService;
import com.example.com.order.mapper.OrderMapper;
import com.example.com.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServicelmpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private StockFeignService stockFeignService;
    @Override
    public String getUserInfo(){
        return "user接口已调用";
    }


    @Transactional
    @GlobalTransactional
    @Override
    public Order createOrder(Order order) {
//        // 扣库存
//        String result = stockFeignService.reduceStock(productId, 1);
//        if (!"success".equals(result)) {
//            throw new RuntimeException("库存不足");
//        }
//
//        // 创建订单
//        Order order = new Order();
//        order.setProductId(productId);
//        orderMapper.insert(order);
//
//        return "订单创建成功，订单ID：" + order.getId();

        orderMapper.insert(order);
        String msg = stockFeignService.reduceStock(order.getProductId());
        System.out.println(msg);
        //故意异常
        int i =1/0;
        return order;
    }

}
